#!/usr/bin/python3
# -*- coding: utf-8 -*-

import numpy  as np
import pandas as pd
import talib as ta

eps = 1e-8

def signal(*args):
    # MarketPl指标
    df = args[0]
    n = args[1]
    factor_name = args[2]

    quote_volume_ema = df['quote_volume'].ewm(span=n, adjust=False).mean()
    volume_ema = df['volume'].ewm(span=n, adjust=False).mean()
    df['平均持仓成本'] = quote_volume_ema / volume_ema
    df[factor_name] = df['close'] / (df['平均持仓成本'] + eps) - 1

    del df['平均持仓成本']

    return df
